package com.qding.park.adapter.report.controller.v1;

import java.util.List;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.qding.park.adapter.report.constants.WebConstants;
import com.qding.park.adapter.report.controller.WebController;
import com.qding.park.adapter.report.controller.v1.form.ErrorParkOutForm;
import com.qding.park.adapter.report.controller.v1.form.ParkEventSyncForm;
import com.qding.park.adapter.report.controller.v1.form.ParkInSyncForm;
import com.qding.park.adapter.report.controller.v1.form.ParkOutRenewalForm;
import com.qding.park.adapter.report.controller.v1.form.ParkOutSyncForm;
import com.qding.park.adapter.report.controller.v1.form.RenewalSyncForm;
import com.qding.park.adapter.report.util.JsonObjectResp;
import com.qding.park.adapter.report.valid.ValidateUtil;
import com.qding.park.common.core.cache.service.CacheServiceFacade;
import com.qding.park.common.core.constant.CacheKeyThree;
import com.qding.park.common.core.exceptions.IExceptionHandle;
import com.qding.park.common.utils.BeanUtils;
import com.qding.park.common.web.controller.BaseController;
import com.qding.park.common.web.domain.AnswerMsg;
import com.qding.park.manager.facade.queue.service.entity.ExceptionParkOutSyncDTO;
import com.qding.park.manager.facade.queue.service.entity.ParkEventSyncDTO;
import com.qding.park.manager.facade.queue.service.entity.ParkInSyncDTO;
import com.qding.park.manager.facade.queue.service.entity.ParkOutRenewalSyncDTO;
import com.qding.park.manager.facade.queue.service.entity.ParkOutSyncDTO;
import com.qding.park.manager.facade.queue.service.entity.RenewalSyncDTO;
import com.qding.park.manager.pojo.queue.QueueMsg;
import com.qding.park.manager.pojo.queue.QueueTypeEnum;

@RestController
@RequestMapping(WebConstants.API_1_0_PREFIX)
public class ReportController_v1 extends WebController {
    
    /**
     * @Field @cacheServiceFacade : TODO(缓存服务)
     */
    @Autowired
    private CacheServiceFacade cacheServiceFacade;

	/**
	 * @Description (车牌/卡续费同步)
	 * @param form
	 * @return
	 */
    @RequestMapping("/renewalSync")
    @ResponseBody
	public JsonObjectResp renewalSync(@RequestBody @Valid RenewalSyncForm  form) {
	    
	    if(_log.isDebugEnabled()){
	        _log.debug(form.toString());
	    }
	   
	    /*属性复制*/
	    RenewalSyncDTO renewalSyncDTO =new RenewalSyncDTO();
        BeanUtils.copyProperties(form, renewalSyncDTO);
	    
        /*发送队列消息*/
        QueueMsg msg = new QueueMsg();
        msg.setEventType(QueueTypeEnum.RENEWALSYNC.getType());
        msg.setData(renewalSyncDTO);
        cacheServiceFacade.msg_queue_push(msg, CacheKeyThree.QUEUE_REPORT);
	    return buildSuccessResponse();
	    
	}
    
    /**
     * @Description (入场记录上报)
     * @param form
     * @return
     */
    @RequestMapping("/parkInSync")
    @ResponseBody
    public JsonObjectResp parkInSync(@Valid @RequestBody  List<ParkInSyncForm>  forms) {
        
        if(_log.isDebugEnabled()){
            _log.debug(forms.toString());
            _log.debug("size="+forms.size());
        }
        
        for(int i=0;i<forms.size();i++){
            ValidateUtil.validate(forms.get(i),String.valueOf(i));
        }
        
        for(ParkInSyncForm form:forms){
            /*属性复制*/
            ParkInSyncDTO parkInSyncDTO =new ParkInSyncDTO();
            BeanUtils.copyProperties(form, parkInSyncDTO);
            
            /*发送队列消息*/
            QueueMsg msg = new QueueMsg();
            msg.setEventType(QueueTypeEnum.PARKINSYNC.getType());
            msg.setData(parkInSyncDTO);
            cacheServiceFacade.msg_queue_push(msg, CacheKeyThree.QUEUE_REPORT);
        }
        
        return buildSuccessResponse();
        
    }
    
    /**
     * @Description (TODO异常出场记录上报)
     * @return
     */
    @RequestMapping("/exceptionParkOutSync")
    @ResponseBody
    public JsonObjectResp  exceptionParkOutSync(@Valid @RequestBody List<ErrorParkOutForm> forms){
        if(_log.isDebugEnabled()){
            _log.debug(forms.toString());
            _log.debug("size="+forms.size());
        }
        for(int i=0;i<forms.size();i++){
            ValidateUtil.validate(forms.get(i),String.valueOf(i));
        }
        
        for(ErrorParkOutForm form:forms){
            /*属性复制*/
            ExceptionParkOutSyncDTO exceptionParkOutSyncDTO =new ExceptionParkOutSyncDTO();
            BeanUtils.copyProperties(form, exceptionParkOutSyncDTO);
            
            /*发送队列消息*/
            QueueMsg msg = new QueueMsg();
            msg.setEventType(QueueTypeEnum.EXCEPTION_PARKOUTSYNC.getType());
            msg.setData(exceptionParkOutSyncDTO);
            cacheServiceFacade.msg_queue_push(msg, CacheKeyThree.QUEUE_REPORT);
        }
        return buildSuccessResponse();
    }
    
    
    /**
     * @Description (TODO出场记录上报上报)
     * @return
     */
    @RequestMapping("/parkOutSync")
    @ResponseBody
    public JsonObjectResp  parkOutSync(@Valid @RequestBody List<ParkOutSyncForm> forms){
        if(_log.isDebugEnabled()){
            _log.debug(forms.toString());
            _log.debug("size="+forms.size());
        }
        for(int i=0;i<forms.size();i++){
            ValidateUtil.validate(forms.get(i),String.valueOf(i));
        }
        
        
        for(ParkOutSyncForm form:forms){
            /*属性复制*/
            ParkOutSyncDTO parkOutSyncDTO =new ParkOutSyncDTO();
            BeanUtils.copyProperties(form, parkOutSyncDTO);
            
            /*发送队列消息*/
            QueueMsg msg = new QueueMsg();
            msg.setEventType(QueueTypeEnum.PARKOUTSYNC.getType());
            msg.setData(parkOutSyncDTO);
            cacheServiceFacade.msg_queue_push(msg, CacheKeyThree.QUEUE_REPORT);
        }
        return buildSuccessResponse();
    }
    
    /**
     * @Description (TODO出/入场缴费记录)
     * @param forms
     * @return
     */
    @RequestMapping("/parkOutRenewalSync")
    @ResponseBody
    public JsonObjectResp  parkOutRenewalSync(@Valid @RequestBody List<ParkOutRenewalForm> forms){
        if(_log.isDebugEnabled()){
            _log.debug(forms.toString());
            _log.debug("size="+forms.size());
        }
        for(int i=0;i<forms.size();i++){
            ValidateUtil.validate(forms.get(i),String.valueOf(i));
        }
        
        
        for(ParkOutRenewalForm form:forms){
            /*属性复制*/
            ParkOutRenewalSyncDTO parkOutRenewalSyncDTO =new ParkOutRenewalSyncDTO();
            BeanUtils.copyProperties(form, parkOutRenewalSyncDTO);
            
            /*发送队列消息*/
            QueueMsg msg = new QueueMsg();
            msg.setEventType(QueueTypeEnum.PARKOUT_RENEWALSYNC.getType());
            msg.setData(parkOutRenewalSyncDTO);
            cacheServiceFacade.msg_queue_push(msg, CacheKeyThree.QUEUE_REPORT);
        }
        
        return buildSuccessResponse();
    }
    
    /**
     * @Description (TODO事件推送)
     * @param forms
     * @return
     */
    @RequestMapping("/parkEventSync")
    @ResponseBody
    public JsonObjectResp  parkEventSync(@Valid @RequestBody ParkEventSyncForm form){
        if(_log.isDebugEnabled()){
            _log.debug(form.toString());
        }
        
        ParkEventSyncDTO parkEventSyncDTO=new ParkEventSyncDTO();
        BeanUtils.copyProperties(form, parkEventSyncDTO);
        parkEventSyncDTO.setAlarmTime(form.getEventBody().getAlarmTime());
        parkEventSyncDTO.setCarNum(form.getEventBody().getCarNum());
        parkEventSyncDTO.setEnInDevice(form.getEventBody().getEnInDevice());
        parkEventSyncDTO.setEnterTime(form.getEventBody().getEnterTime());
        parkEventSyncDTO.setOutDevice(form.getEventBody().getOutDevice());
        parkEventSyncDTO.setParkCode(form.getEventBody().getParkCode());
        
        
        /*发送队列消息*/
        QueueMsg msg = new QueueMsg();
        msg.setEventType(QueueTypeEnum.PARKEVENTSYNC.getType());
        msg.setData(parkEventSyncDTO);
        cacheServiceFacade.msg_queue_push(msg, CacheKeyThree.QUEUE_REPORT);
        return buildSuccessResponse();
    }
}
